﻿using Autofac;
using Ewell.BizComponent.UI.Core.Model;
using Ewell.BizComponent.UI.Core.VievModel;
using Ewell.BizComponent.UI.Reg.ViewModel;
using Ewell.BizComponent.UI.Reg.Views;
using Ewell.Interface.Common.Model.Common;
using Ewell.Interface.Common.Model.GateWay.Dto.Basic;
using Ewell.Interface.Common.Model.GateWay.Dto.Dict;
using Ewell.Interface.Common.Model.GateWay.GateWayUriInfo;
using Ewell.Interface.Common.Utils;
using JetBrains.Annotations;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Ewell.BizComponent.UI.Reg.Windows
{
    /// <summary>
    /// OutRegWindow.xaml 的交互逻辑
    /// </summary>
    public partial class OutRegWindow : MetroWindow
    {
        public IDialogCoordinator dialogCoordinator { get; set; } = DialogCoordinator.Instance;
        private OutRegVm _outRegVm;
        public OutRegVm outRegVm { 
            get=>_outRegVm;
            set
            {
                if(_outRegVm != value)
                {
                    _outRegVm = value;
                    OnPropertyChanged(nameof(outRegVm));
                }
            }
        }
        public OutRegWindow()
        {
            InitializeComponent();
            if (Global.RuntimeEnv != "System")
            {
                this.ResizeMode=ResizeMode.NoResize;
                this.ShowCloseButton = false;
            }
            this.DataContext = this;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (outRegVm.bizOutRegisterRequest!=null)
            {
                #region 查询患者信息
                if (!string.IsNullOrWhiteSpace(outRegVm.bizOutRegisterRequest.patientId))
                {
                    PersonDubboService personDubboService = new PersonDubboService();
                    PersonPatientQueryRequest patientQueryRequest = new PersonPatientQueryRequest()
                    {
                        patientIds = new List<string>() { outRegVm.bizOutRegisterRequest.patientId },
                        personInfoTypes = new List<string>() { "2" },
                    };
                    var gateWayPatList = personDubboService.queryPersonByExample.Post<List<PatientVm>>(patientQueryRequest).CheckState("诊间挂号");
                    if (gateWayPatList.@object == null || gateWayPatList.@object.Count == 0) throw new SpeedException("诊间支付", "未查询到患者信息!");
                    outRegVm.patient = gateWayPatList.@object.FirstOrDefault();
                }
                #endregion

                #region 科室编码转换
                if (!string.IsNullOrWhiteSpace(outRegVm.bizOutRegisterRequest.deptCode))
                {
                    DictDivisionQueryRequest queryDivReq = new DictDivisionQueryRequest()
                    {
                        codes = new List<string>() { }
                    };
                    DictDivisionDubboService dictDivisionDubboService = new DictDivisionDubboService();
                    var gateWayDiv = dictDivisionDubboService.queryPageDictDivision.Post<List<DictDivisionResponse>>(queryDivReq);
                    if (!gateWayDiv.success) throw new SpeedException("诊间挂号", gateWayDiv.message);
                    if (gateWayDiv.@object?.Count <= 0) throw new SpeedException("诊间挂号", $"未查询到{outRegVm.bizOutRegisterRequest.deptCode}的科室!");
                    outRegVm.bizOutRegisterRequest.deptId = gateWayDiv.@object.FirstOrDefault().no;
                }
                #endregion

                #region 人员编码转换
                if (!string.IsNullOrWhiteSpace(outRegVm.bizOutRegisterRequest.docCode))
                {
                    DictUserQueryRequest userQueryRequest = new DictUserQueryRequest()
                    {
                        codes = new List<string>() { outRegVm.bizOutRegisterRequest.docCode }
                    };
                    DictCloudUserDubboService dictCloudUserDubboService = new DictCloudUserDubboService();
                    var gateWayUser = dictCloudUserDubboService.queryPageDictUser.Post<List<DictUserResponse>>(userQueryRequest);
                    if (!gateWayUser.success) throw new SpeedException("诊间挂号", gateWayUser.message);
                    if (gateWayUser.@object?.Count <= 0) throw new SpeedException("诊间挂号", $"未查询到{outRegVm.bizOutRegisterRequest.docCode}的医生!");
                    outRegVm.bizOutRegisterRequest.docId = gateWayUser.@object.FirstOrDefault().no;
                }
                #endregion
            }
            //if (outRegVm == null)
            //    outRegVm = Global.Container.Resolve<OutRegVm>();
            if (outRegVm==null) 
                outRegVm= new OutRegVm();
            outRegVm.mainWindow = this;
        }
    }
}
